import { _decorator, Component, Node, Prefab, instantiate, director } from 'cc'
import { ApiMsgEnum, IApiPlayerListRes, IApiRoomListRes } from '../Common'
import { EventEnum, SceneEnum } from '../Enum'
import DataManager from '../Global/DataManager'
import EventManager from '../Global/EventManager'
import { NetworkManager } from '../Global/NetworkManager'
import { PlayerManager } from '../UI/PlayerManager'
import { RoomManager } from '../UI/RoomManager'

const { ccclass, property } = _decorator

// 大厅场景
@ccclass('HallManager')
export class HallManager extends Component {
  @property(Node)
  playerContainer: Node

  @property(Prefab)
  playerPrefab: Prefab

  @property(Node)
  roomContainer: Node

  @property(Prefab)
  roomPrefab: Prefab

  onLoad() {
    // 加入房间事件处理
    EventManager.Instance.on(EventEnum.RoomJoin, this.handleJoinRoom, this)
    // 监听同步玩家列表
    NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgPlayerList, this.renderPlayers, this)
    // 监听同步房间列表
    NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgRoomList, this.renderRooms, this)
  }

  start() {
    // 清空节点
    this.playerContainer.destroyAllChildren()
    this.roomContainer.destroyAllChildren()
    this.getPlayers()
    this.getRooms()
    // 场景预载
    director.preloadScene(SceneEnum.Room)
  }
  onDestroy() {
    NetworkManager.Instance.unListenMsg(ApiMsgEnum.MsgPlayerList, this.renderPlayers, this)
    NetworkManager.Instance.unListenMsg(ApiMsgEnum.MsgRoomList, this.renderRooms, this)
  }

  // 获取玩家列表
  async getPlayers() {
    const { success, error, res } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiPlayerList, {})
    if (!success) return console.log('api错误：', error)
    this.renderPlayers(res)
  }
  // 获取房间列表
  async getRooms() {
    const { success, error, res } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomList, {})
    if (!success) return console.log('api错误：', error)
    this.renderRooms(res)
  }
  // 渲染玩家列表
  renderPlayers({ list }: IApiPlayerListRes) {
    // 隐藏所有节点
    for (const child of this.playerContainer.children) {
      child.active = false
    }
    // 生成等长节点
    while (this.playerContainer.children.length < list.length) {
      const node = instantiate(this.playerPrefab)
      node.active = false
      node.setParent(this.playerContainer)
    }
    // 节点显示和赋值
    for (let i = 0; i < list.length; i++) {
      const data = list[i]
      const node = this.playerContainer.children[i]
      node.getComponent(PlayerManager).init(data)
    }
  }
  // 渲染房间列表
  renderRooms({ list }: IApiRoomListRes) {
    // 隐藏所有节点
    for (const child of this.roomContainer.children) {
      child.active = false
    }
    // 生成等长节点
    while (this.roomContainer.children.length < list.length) {
      const node = instantiate(this.roomPrefab)
      node.active = false
      node.setParent(this.roomContainer)
    }
    // 节点显示和赋值
    for (let i = 0; i < list.length; i++) {
      const data = list[i]
      const node = this.roomContainer.children[i]
      node.getComponent(RoomManager).init(data)
    }
  }
  // 创建房间
  async handleCreateRoom() {
    const { success, error, res } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomCreate, {})
    if (!success) return console.log('api错误：', error)
    DataManager.Instance.roomInfo = res.room
    // 场景切换
    director.loadScene(SceneEnum.Room)
  }
  // 加入房间
  async handleJoinRoom(rid: number) {
    const { success, error, res } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomJoin, { rid })
    if (!success) return console.log('api错误：', error)
    DataManager.Instance.roomInfo = res.room

    // 场景切换
    director.loadScene(SceneEnum.Room)
  }
}
